<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <div>{{msg}}<button @click="handle">点击我吧</button></div>
    <jerry></jerry>
    <tom></tom>
  </div>
  <script src="../js/vue.js"></script>
  <script>
    const hub = new Vue(); //事件中心
    
    Vue.component("tom", {
      data() {
        return {
          "num": 0
        }
      },
      mounted() {
        hub.$on("toms", (val) => { //注册交互事件
          this.num += val;
        });
      },
      "methods": {
        handle() {
          // 触发兄弟之间的事件
          hub.$emit("jerrys", 2);
        }
      },
      "template": `
    <div>
      <div>tom:{{num}}</div>
      <div>
        <button @click="handle">点击</button>
      </div>
    </div>`
    });
    Vue.component("jerry", {
      data() {
        return {
          "num": 0
        }
      },
      mounted() {
        hub.$on("jerrys", (val) => { //注册交互事件
          this.num += val;
        });
      },
      "methods": {
        handle() {
          // 触发兄弟之间的事件
          hub.$emit("toms", 1);
        }
      },
      "template": `
        <div>
          <div>Jerry:{{num}}</div>
          <div>
              <button @click="handle">点击</button>
          </div>
        </div>`
    });
    
    const vm = new Vue({
      "el": "#app",
      "data": {
        "msg": "大家好！我是你们的父组件"
      },
      mounted() {},
      updated() {},
      "methods": {
        handle() {
          hub.$off("toms"); //销毁注册的事件
          hub.$off("jerrys");
        }
      },
      "directives": {},
      "watch": {},
      "filters": {},
      "components": {

      }
    });
  </script>
</body>

</html>